<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item label="name" prop="name">
        <el-input v-model="temp.name" />
      </el-form-item>
      <el-form-item label="value" prop="value">
        <el-input v-model="temp.value" />
      </el-form-item>
      <el-form-item label="删除标志" prop="deleteflag">
        <select-option placeholder="请选择"
          v-model="temp.deleteflag"
          :clearable=true
          prop-name="deleteflag"
          :prop-ds-type=1
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  createApi, updateApi,
  select4enums
} from './api'

// 导入多选项组件
import SelectOption from '@/views/coding/components/SelectOption'


export default {
    name: 'TmptUserSettingAdd',
    components: { SelectOption },
    props: {
        isFromFather: {
            type: Boolean,
            default: false
        },
        row: {}
    },

    data() {
        return {
            rules: {
                name: [{ required: true, message: 'required', trigger: 'blur' }],
                value: [{ required: true, message: 'required', trigger: 'blur' }],
                deleteflag: [{ required: true, message: 'required', trigger: 'blur' }],
            },
            temp: {},
            deleteflagList: [],
            listLoading: true
        }
    },

    watch: {
        'row': {
            immediate: true, // vue首次赋值不触发watch的解决方法
            handler: function(nval){
                if (this.isFromFather) {
                    this.temp = Object.assign({}, this.row) // copy obj
                }
            }
        }
    },

    mounted() {
    },

    methods: {
        resetTemp() {
            this.temp = {
                name: '',
                value: '',
                deleteflag: '',
                id: undefined
            }
        },
        // 树表： 拿当前节点ID作为父节点ID，建立树关系
        setParentNodeId(id) {
            this.temp.tmptUserSettingId = id
        },
        createData() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    const param = Object.assign({}, this.temp)
                    createApi(param).then((r) => {
                        this.notify(r)
                        if (r.success) {
                            this.resetTemp()
                            // 从父组件调用此方法，进行回调下
                            if (this.isFromFather) {
                                this.$emit('createResult')
                            }
                        }
                    })
                }
            })
        },
        updateData() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    const param = { id:this.temp.id }
                    param.name = this.temp.name
                    param.value = this.temp.value
                    param.deleteflag = this.temp.deleteflag
                    updateApi(param).then((r) => {
                        this.notify(r)
                        if (r.success) {
                            this.resetTemp()
                            // 从父组件调用此方法，进行回调下
                            if (this.isFromFather) {
                                this.$emit('updateResult')
                            }
                        }
                    })
                }
            })
        },
        clearValidate() {
            this.$refs['dataForm'].clearValidate()
        }
    }

}
</script>
<style>

</style>
